package com.block.api.controller.source;

import com.alibaba.fastjson.JSONObject;
import com.block.common.model.dto.SourceCountDTO;
import com.block.common.model.vo.ResultInfoVO;
import com.block.common.service.ISourceDataCountInfoService;
import com.block.common.service.LogFactory;
import com.block.db.entity.ApplicationInfo;
import com.block.db.entity.ChannelInfo;
import com.block.db.entity.SourceBaseInfo;
import com.block.source.service.ISourceCheckUserService;
import com.block.source.service.ISourceHandleCheckUserService;
import com.block.source.service.ISourceRegisterUserService;
import com.block.test.reqbean.source.CheckUserInfo;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * url 跳转授权
 * 半流程
 *
 */
@RestController
@RequestMapping("apisource/apib")
public class SourceApiBDataController {

    @Autowired
    ISourceHandleCheckUserService sourceHandleCheckUserService;

    private static final Logger log = LogFactory.getSourceLog();

    @Autowired
    ISourceCheckUserService sourceCheckUserService;
    @Autowired
    ISourceRegisterUserService sourceRegisterUserService;
    @Autowired
    ISourceDataCountInfoService sourceDataCountInfoService;

    @RequestMapping(value = "/checkUser", method = {RequestMethod.POST})
    public String checkUser(@RequestAttribute(value = "tranData") JSONObject tranData,
                            @RequestAttribute(value = "applicationInfo") ApplicationInfo applicationInfo,
                            @RequestAttribute(value = "channelInfo") ChannelInfo channelInfo,
                            @RequestAttribute(value = "sourceBaseInfo") SourceBaseInfo sourceBaseInfo,
                            @RequestAttribute(value = "tranDataBean") CheckUserInfo checkUserInfo,
                            @RequestAttribute(value = "uri") String uri,
                            @RequestAttribute(value = "ip") String ip) {

        SourceCountDTO sourceCountDTO = new SourceCountDTO(channelInfo.getChannelCode(), channelInfo.getChannelName());
        sourceCountDTO.setCheckNumber(1);
        sourceDataCountInfoService.saveSourceCountInfo(sourceCountDTO);

        checkUserInfo.setUserIp(ip);
        ResultInfoVO resultInfoVO = sourceCheckUserService.sourceCheckUser1(sourceBaseInfo, checkUserInfo, channelInfo, applicationInfo);

        return resultInfoVO.toResp();
    }

    @RequestMapping(value = "/registerUser", method = {RequestMethod.POST})
    public String registerUser(@RequestAttribute(value = "tranData") JSONObject tranData,
                               @RequestAttribute(value = "applicationInfo") ApplicationInfo applicationInfo,
                               @RequestAttribute(value = "channelInfo") ChannelInfo channelInfo,
                               @RequestAttribute(value = "sourceBaseInfo") SourceBaseInfo sourceBaseInfo,
                               @RequestAttribute(value = "tranDataBean") CheckUserInfo checkUserInfo,
                               @RequestAttribute(value = "uri") String uri,
                               @RequestAttribute(value = "ip") String ip) {

        checkUserInfo.setUserIp(ip);

        SourceCountDTO sourceCountDTO = new SourceCountDTO(sourceBaseInfo.getChannelCode(), sourceBaseInfo.getChannelName());
        sourceCountDTO.setRegisterNumber(1);
        sourceDataCountInfoService.saveSourceCountInfo(sourceCountDTO);

        String resultNo = checkUserInfo.getOrderNo();
        ResultInfoVO resultInfoVO = sourceRegisterUserService.sourceRegisterUser1(resultNo, sourceBaseInfo, checkUserInfo);

        if (resultInfoVO.isSuccess()){
            sourceCountDTO.setRegisterPassNumber(1);
            sourceCountDTO.setRegisterNumber(null);
            sourceDataCountInfoService.saveSourceCountInfo(sourceCountDTO);
        }

        return resultInfoVO.toResp();
    }
}
